// DoesThisWork.cpp : main project file.

#include "stdafx.h"
#include "WinFormMezzoForte.h"

// Buffers hold sound data.
ALuint Buffer[16];

// Sources are points emitting sound.
ALuint Source[16];

// Position of the source sound.
ALfloat SourcePos[] = { 0.0, 0.0, 0.0 };

// Velocity of the source sound.
ALfloat SourceVel[] = { 0.0, 0.0, 0.0 };


// Position of the listener.
ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 };

// Velocity of the listener.
ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };

// Orientation of the listener. (first 3 elements are "at", second 3 are "up")
ALfloat ListenerOri[] = { 0.0, 0.0, -1.0,  0.0, 1.0, 0.0 };

ALenum format;
ALsizei size;
ALvoid* data;
ALsizei freq;
ALboolean loop;


int LoadALData(int note, int oct)
{
	char fileName[100];
	int halfStep[12] = {9, 10, 11, 0, 1, 2, 3, 4, 5, 6, 7, 8};
	double modifier;
	ALenum state;
	static int cleared = 0;
	static int rotation = 0;

    if(cleared)
	{

		alSourceStop(Source[rotation]);

		alSourceUnqueueBuffers(Source[rotation], 1, &Buffer[rotation]);
		alDeleteBuffers(1, &Buffer[rotation]);
		alDeleteSources(1, &Source[rotation]);
	}

	alGenSources(1, &Source[rotation]);

	// Load wav data into a buffer.
    alGenBuffers(1, &Buffer[rotation]);

	int me = alGetError();

    /*if (me != AL_NO_ERROR)
        return AL_FALSE;*/

	switch(oct)
	{
	case 2: strncpy_s(fileName, 100, "PianoKeys/Piano Key C2.wav\0", 100); break;
	case 3: strncpy_s(fileName, 100, "PianoKeys/Piano Key C3.wav\0", 100); break;
	case 4: strncpy_s(fileName, 100, "PianoKeys/Piano Key C4.wav\0", 100); break;
	case 5: strncpy_s(fileName, 100, "PianoKeys/Piano Key C5.wav\0", 100); break;
	case 6: strncpy_s(fileName, 100, "PianoKeys/Piano Key C6.wav\0", 100); break;
	case 7: strncpy_s(fileName, 100, "PianoKeys/Piano Key C7.wav\0", 100); break;
	case 8: strncpy_s(fileName, 100, "PianoKeys/Piano Key C8.wav\0", 100); break;
	case 9: strncpy_s(fileName, 100, "PianoKeys/Piano Key C9.wav\0", 100); break;
	default: strncpy_s(fileName, 100, "\0", 100); break;
	}

    alutLoadWAVFile(fileName, &format, &data, &size, &freq, &loop);

	modifier = pow((double)2, (double)halfStep[note]/12);
	freq = (int)(freq * modifier);

    alBufferData(Buffer[rotation], format, data, size, freq);

	

    alutUnloadWAV(format, data, size, freq);

    if (alGetError() != AL_NO_ERROR)
        return -1;

    alSourcei (Source[rotation], AL_BUFFER,   Buffer[rotation]   );
    alSourcef (Source[rotation], AL_PITCH,    0.5f     );
    alSourcef (Source[rotation], AL_GAIN,     1.0f     );
    alSourcefv(Source[rotation], AL_POSITION, SourcePos);
    alSourcefv(Source[rotation], AL_VELOCITY, SourceVel);
    alSourcei (Source[rotation], AL_LOOPING,  loop     );

	rotation++;

	if(rotation == 16)
	{
		rotation = 0;
		cleared = 1;
	}

	// Do another error check and return.
    if (alGetError() == AL_NO_ERROR)
        return (rotation - 1);

    return -1;
}

void SetListenerValues()
{
    alListenerfv(AL_POSITION,    ListenerPos);
    alListenerfv(AL_VELOCITY,    ListenerVel);
    alListenerfv(AL_ORIENTATION, ListenerOri);
}

ALboolean ModifyFrequency(int note, int octave)
{

	return AL_FALSE;
}

void KillALData()
{
    alDeleteBuffers(16, Buffer);
    alDeleteSources(16, Source);
    alutExit();
}

using namespace DoesThisWork;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
	int argc = 1;
	char argv[20] = "PC Software.exe\0";

	

	alutInit(&argc, (char**) &argv);
    alGetError();

	// Bind buffer with a source.

    SetListenerValues();

    // Setup an exit procedure.
    atexit(KillALData);

	LoadALData(0, 4);

	// Enabling Windows XP visual effects before any controls are created
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 

	// Create the main window and run it
	Application::Run(gcnew WinFormMezzoForte());
	return 0;
}
